import {NgModule, Optional, SkipSelf} from '@angular/core';
import {CommonModule} from '@angular/common';
import {HttpClientModule, HTTP_INTERCEPTORS} from '@angular/common/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {BrowserModule} from '@angular/platform-browser';
import {registerLocaleData} from '@angular/common';
import {NZ_I18N, zh_CN} from 'ng-zorro-antd';
import zh from '@angular/common/locales/zh';

import {AppRoutingModule} from '../app-routing.module';
import {ShareModule} from '../share/share.module';
import {PagesModule} from '../pages/pages.module';
import {ServicesModule} from '../services/services.module';
import {AppStoreModule} from '../store';
import {HttpRequestInterceptor, HttpResponseInterceptor} from '../helpers/http.interceptor';

registerLocaleData(zh);

@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    BrowserModule,
    HttpClientModule,
    BrowserAnimationsModule,

    /* 自定义模块 */
    ServicesModule,
    PagesModule,
    ShareModule,

    /* ngrx */
    AppStoreModule,

    /* 路由模块 */
    AppRoutingModule,
  ],
  exports: [
    ShareModule,
    AppRoutingModule
  ],
  providers: [
    {
      provide: NZ_I18N,
      useValue: zh_CN
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpRequestInterceptor,
      multi: true
    }
    ,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpResponseInterceptor,
      multi: true
    }
  ],
})
export class CoreModule {
  // 只允许 AppModule 引入，其他模块不允许引入 CoreModule
  // 避免无限循环，允许不存在
  constructor(@SkipSelf() @Optional() parentModule: CoreModule) {
    if (parentModule) {
      throw new Error('CoreModule 只能被 AppModule 引入');
    }
  }
}
